package application.common.util;

import java.util.Date;

import com.jfinal.ext.kit.DateKit;

import application.common.model.Cabinet;
import application.common.model.User;
import application.mvc.biz.authorize.AuthorizeController;
import application.mvc.biz.cabinet.CabinetController;
import application.mvc.biz.group.CabGroupController;
import application.mvc.biz.message.MessageController;
import application.mvc.sys.config.fpr.FprController;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class RouteUtil {

	public static Stage mainStage;
	public static int time = ShiroUtil.getTimeout();

	public static void init(Stage stage) {
		mainStage = stage;
		mainStage.initStyle(StageStyle.UNDECORATED);// 设定窗口无边框
		goInit();
		mainStage.show();
		mainStage.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
			public void handle(MouseEvent mouseEvent) {
				time = ShiroUtil.getTimeout();
			}
		});
	}

	public static void goInit() {
		try {
			Parent root = FXMLLoader.load(RouteUtil.class.getResource("/application/mvc/sys/init/InitScene.fxml"));
			mainStage.setScene(new Scene(root));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	public static void goUpdate(String id, String version) {
//		Platform.runLater(new Runnable() {
//			public void run() {
//				try {
//					time = ShiroUtil.getTimeout();
//					FXMLLoader loader = new FXMLLoader(
//							RouteUtil.class.getResource("/application/mvc/sys/update/UpdateScene.fxml"));
//					UpdateController controller = new UpdateController(id, version);
//					loader.setController(controller);
//					AnchorPane root = loader.load();
//					mainStage.setScene(new Scene(root));
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//
//	}

	public static void goIndex() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					try {
						ShiroUtil.getConfig().setState("0");
						ShiroUtil.getConfig().update();
						CamreaUtil.stopRecordCamrea();
					} catch (Exception e) {
					}
					ShiroUtil.setUser(null);
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/index/IndexScene.fxml"));
					mainStage.setScene(new Scene(root));
					try {
						if (ShiroUtil.getCameraRecord() != null) {
							ShiroUtil.getCameraRecord().setStopTime(
									DateKit.toStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
							ShiroUtil.getCameraRecord().update();
							ShiroUtil.setCameraRecord(null);
						}
					} catch (Exception e) {
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goLogin() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/login/LoginScene.fxml"));
					mainStage.setScene(new Scene(root));
					CamreaUtil.startRecordCamrea();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goArchives() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/biz/archive/Archives.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goScanner() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/biz/scanner/Scanner.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goCabGroup(String archiveNo, boolean open) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					FXMLLoader loader = new FXMLLoader(
							RouteUtil.class.getResource("/application/mvc/biz/group/CabGroup.fxml"));
					CabGroupController controller = new CabGroupController(archiveNo, open);
					loader.setController(controller);
					AnchorPane root = loader.load();
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goCabinet(String archiveNo, Cabinet cabinet) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					FXMLLoader loader = new FXMLLoader(
							RouteUtil.class.getResource("/application/mvc/biz/cabinet/Cabinet.fxml"));
					CabinetController controller = new CabinetController(archiveNo, cabinet);
					loader.setController(controller);
					AnchorPane root = loader.load();
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goAuthorize(String storeId) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					FXMLLoader loader = new FXMLLoader(
							RouteUtil.class.getResource("/application/mvc/biz/authorize/Authorize.fxml"));
					AuthorizeController controller = new AuthorizeController(storeId);
					loader.setController(controller);
					AnchorPane root = loader.load();
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goUsers() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/user/Users.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goLogs() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/log/LogScene.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goFpr(User user) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					FXMLLoader loader = new FXMLLoader(
							RouteUtil.class.getResource("/application/mvc/sys/config/fpr/FprScene.fxml"));
					FprController controller = new FprController(user);
					loader.setController(controller);
					AnchorPane root = loader.load();
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goConfig() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/ConfigScene.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goBaseConfig() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/base/BaseConfigScene.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goSettingBox() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/box/BoxScene.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goSettingCamera() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/camera/CameraScene.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goMessage(int type, String message, String srcPage) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					FXMLLoader loader = new FXMLLoader(
							RouteUtil.class.getResource("/application/mvc/biz/message/MessageScene.fxml"));
					MessageController controller = new MessageController(type, message, srcPage);
					loader.setController(controller);
					AnchorPane root = loader.load();
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void goFault() {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					time = ShiroUtil.getTimeout();
					Parent root = FXMLLoader
							.load(RouteUtil.class.getResource("/application/mvc/sys/config/fault/Fault.fxml"));
					mainStage.setScene(new Scene(root));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void close() {
		if (mainStage != null) {
			mainStage.close();
		}
	}

	public static int getTime() {
		return time;
	}

	public static synchronized void setTime(int time) {
		RouteUtil.time = time;
	}

}
